var agr = agr || {};


/**
 * @constructs listView
 * @param {jQuery}
 *            $obj transformed to listview.
 */
agr.ListView = function($obj) {
    var listView = {};

    (function($obj) {
        'use strict';

        // holds each item in the list as a name:item pair
        var listItems = {};
        var $listItems = {};
        // States
        listView.edit = false;
	listView.ableDelete = true;
        var deleteM = false;
        var performance = false;
        //counter for items that didnt provide a uid
        var count = 100;

        var getRemoveFunction = function(uid, shouldconfirm) {
            return (function() {
            	if(shouldconfirm){
			var objectname = listItems[uid].html.find('h3').html();
            		var sure = confirm("Are you sure you wish to delete "+objectname+"?");
            		if(!sure){
            			return;
            		}
            	}
	            listItems[uid].active = false;
	             
	            listView.render();
	             
            });
        }
        
        var newAnchor = function(listItem) {
            var $newAnchor = $('<a/>');
            if (listItem.html) {
                $newAnchor.append(listItem.html);
            }
	    if (!listItem.disable){
		    if (listItem.href) {
			$newAnchor.attr('href', listItem.href);
		    }
	    }
            return $newAnchor;
        }
        //internal
        var editModeAppend = function(listItem) {
            var $newItem = $('<li/>');
            var $newAnchor = newAnchor(listItem);
            if (!listItem.disable) {
                if (listItem.edit) {
                    $newAnchor.attr('href','');
                    $newAnchor.click(listItem.edit);
                }
                if (listItem.edithref){
                	$newAnchor.attr("href",listItem.edithref)
                }
            }
            $newItem.append($newAnchor);
	    if(listView.ableDelete){
		    $newItem.append($('<a/>',{
			click : getRemoveFunction(listItem.id,true)
		    }));
	    }
            return $newItem;

        }
        //internal
        var deleteModeAppend = function(listItem) {
            var $newItem = $('<li/>', {
                'data-icon': 'delete'
            });
            var $newAnchor = newAnchor(listItem);
            if (!listItem.disable) {
                $newAnchor.click(getRemoveFunction(listItem.id));
            }
            $newItem.append($newAnchor);
            return $newItem;
        }
        //internal
        var append = function(listItem) {
            var $newItem = $('<li/>');
            var $newAnchor = newAnchor(listItem);
            if (!listItem.disable) {
                $newAnchor.click(listItem.action);
            }
            $newItem.append($newAnchor);
            return $newItem;
        }
        /**
         * Repopulates the listview to contain only active items and causes a
         * refresh
         */
        listView.render = function() {
            var i;
            $obj.children().remove();
            $listItems = {};
            for (i in listItems) {
                if (listItems[i].active) { // its active
                    var $newItem;
                    if (listItems[i].divider) {
                        $newItem = $('<li/>', {
                            'data-role' : 'list-divider',
                            html: listItems[i].html
                        });
                    } else {
                        if (listView.edit) {
                            $newItem = editModeAppend(listItems[i]);
                        }
                        else if (deleteM) {
                            $newItem = deleteModeAppend(listItems[i]);
                        }
                        else {
                            $newItem = append(listItems[i]);
                        }
                    }
                    $listItems[listItems[i].id] = $newItem;
                    $obj.append($listItems[listItems[i].id]);
                }
            }
            $obj.listview('refresh');
        }

        /**
         * remove item from list
         *
         * @param {Number}
         *            uid
         */
        listView.removeListItem = function(uid) {
            listItems[uid].active = false;
            if(!performance){
           	 listView.render();
            }
        }
        /**
         * Used to add an item to the graphical list
         *
         * @param {{uid:uniqueID, href:String, edithref:String html:String, action:function, edit:function, disable:boolean, divider:boolean, dbObj:Object}} props
         */

        listView.addListItem = function(props) {
            var i;
            if (props.uid) {
            	props.id = props.uid;
                listItems[props.id] = props ;
                i = props.uid;
            }
            else {
                count = count + 1;
                i = count;
                props.id = count;
                listItems[props.id] = props;
            }
            listItems[i].active = true;
            if(!performance){
            	listView.render();
            }
        }

        /**
         *
         * Switches the listView to a split style switching the primary method to the editMethod
         * and adding a delete button to each list item. These delete buttons require confimation
         *
         *  calling this with false will return to original state
         *
         * @param {Boolean}
         *            state true to turn on editMode, false to turn off.
         */
        listView.editOn = function(noDelete) {
            deleteM = false;
            listView.edit = true;
	    listView.ableDelete = !noDelete;
            listView.render();
            return true;
        }
        listView.editOff = function(){
        	listView.edit = false
        	listView.render();
        }
        /**
         * turns on rendering after every add/remove listItem
         * This means you MUST call .render()
         */
        listView.performOn = function() {
            performance = true;
        }
        /**
         * turns off rendering after every add/remove listItem
         * This means you MUST call .render()
         */
        listView.performOff = function(){
        	performance = false;
        }
        /**
         * Switches the listview to a quick deletion state the primary method of each item is changed
         * to set the item as deactive hiding it from the user once clicked.
         *
         * @param {Boolean}
         *            state true to turn on deleteMode, false to turn off.
         */
        listView.deleteMode = function(state) {
            listView.edit = false;
            deleteM = state;
           	listView.render();
        }
        /**
         * @return {Object} raw list state.
         */
        listView.dump = function() {
            return listItems;
        }
        /**
         * Meant for use with dump
         * @param{{Object,Object...}} a list of objects in the same format as required by addListItem
         */
        listView.load = function(list) {
        	listItems = list;
        	listView.render();
        }
    })($obj);
    return listView;
};
